package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.*;
import com.hebeu.gfh.pojo.vo.*;
import com.hebeu.gfh.service.*;
import com.hebeu.gfh.utils.JwtUtil;
import com.hebeu.gfh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/clubs")
public class clubsController {

    @Autowired
    clubsService clubsService;
    @Autowired
    clubMemberService clubMemberService;
    @Autowired
    clubFollowService clubFollowService;
    @Autowired
    clubLikeService  likeService;
    @Autowired
    userService userService;

    //添加社团
    @PostMapping("/addClub")
    public R addClub(addClub addClub){
        Clubs clubs =new Clubs();
        clubs.setClubBadge(addClub.getCImgUrl());
        clubs.setClubName(addClub.getClubName());
        String str = addClub.getIntroduction();
        str = str.replace("<p>", "").replace("</p>", "");
        clubs.setIntroduction(str);
        clubs.setClubTid(addClub.getSelectedTid());
        int userId1 = addClub.getUserNum();
        try {
            //通过userNum查询id
            int userId = userService.getIdByuserNum(userId1);
            //上传社团风采和社团徽章图片到指定路径，并将路径返回，放入clubs中（前端完成）
            clubsService.addClub(clubs);
            //将创建人的信息保存到clubmember表中
            int clubId = clubs.getClubId();
            ClubMember clubMember = new ClubMember();
            clubMember.setClubId(clubId);
            clubMember.setRoleInClub(3);
            clubMember.setUserId(userId);
            clubMemberService.joinClub(clubMember);
            userService.updateRole(1,userId);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("---------------------");
            return R.error(500,"添加社团失败");
        }
        return R.ok("添加社团成功");
    }
    //修改社团
    @PostMapping("/updateClub")
    public R updateClub(@RequestBody Clubs clubs){
        System.out.println("进来了"+clubs.getClubPurpose());
        try {
            clubsService.updateClub(clubs);
            System.out.println("到这了");
        }catch (Exception e){
            System.out.println("报错了");
            System.out.println(e);
            System.out.println("------------------");
            return R.error(500,"修改社团失败");
        }
        return R.ok("修改社团成功");
    }
    //删除社团
    @PostMapping("/deleteClub")
    public R deleteClub(int clubId){
        try {
            clubsService.deleteClub(clubId);
        }catch (Exception e){
            return R.error(500,"删除社团失败");
        }
            return R.ok("删除社团成功");
    }
    //查询所有社团
    @GetMapping("/findAllClub")
    public R findAllClub(pages page){
        List<Clubs> clubsList = clubsService.findAllClub(page);
        List<clubsAndLike> clubsAndLikes = new ArrayList<>();
        for (Clubs club : clubsList) {
            // 获取有多少个赞
            int likeNum = likeService.getLikeNum(club.getClubId());
            // 获取当前用户是否对这个社团点赞
            ClubLike clubLike = new ClubLike();
            clubLike.setClubId(club.getClubId());
            clubLike.setUserId(page.getUserId());
            int isLike = likeService.isLike(clubLike);
            // 创建ClubsAndLike对象，并将其添加到clubsAndLikes列表中
            clubsAndLike clubsAndLike = new clubsAndLike(club, likeNum, isLike);
            clubsAndLikes.add(clubsAndLike);
        }
        int total = clubsService.findClubCount();
        Map  result = new HashMap<>();
        result.put("clubsAndLikes",clubsAndLikes);
        result.put("total",total);
        return R.ok(clubsAndLikes);
    }

    @GetMapping("/findAllClub1")
    public R findAllClub1(pages page){
        List<Clubs> clubsList = clubsService.findAllClub(page);
        List<clubsAndLike> clubsAndLikes = new ArrayList<>();
        for (Clubs club : clubsList) {
            // 获取有多少个赞
            int likeNum = likeService.getLikeNum(club.getClubId());
            // 获取当前用户是否对这个社团点赞
            ClubLike clubLike = new ClubLike();
            clubLike.setClubId(club.getClubId());
            clubLike.setUserId(page.getUserId());
            int isLike = likeService.isLike(clubLike);
            // 创建ClubsAndLike对象，并将其添加到clubsAndLikes列表中
            clubsAndLike clubsAndLike = new clubsAndLike(club, likeNum, isLike);
            clubsAndLikes.add(clubsAndLike);
        }
        int total = clubsService.findClubCount();
        Map  result = new HashMap<>();
        result.put("clubsAndLikes",clubsAndLikes);
        result.put("total",total);
        return R.ok(result);
    }
    //根据社团id查询社团信息
    @PostMapping("/findClubByclubId")
    public R findClubByclubId(ClubFollow clubFollow){
        System.out.println("clubId:"+clubFollow.getClubId());
        Clubs club = clubsService.findClubByclubId(clubFollow.getClubId());
        int followed =clubFollowService.findFollowByuserIdAndClubId(clubFollow);
        ClubsAndFollow clubsAndFollow = new ClubsAndFollow();
        clubsAndFollow.setClubActivity(club.getClubActivity());
        clubsAndFollow.setClubHonors(club.getClubHonors());
        clubsAndFollow.setClubBadge(club.getClubBadge());
        clubsAndFollow.setClubPurpose(club.getClubPurpose());
        clubsAndFollow.setClubName(club.getClubName());
        clubsAndFollow.setClubStyle(club.getClubStyle());
        clubsAndFollow.setClubTid(club.getClubTid());
        clubsAndFollow.setFollowed(followed);
        clubsAndFollow.setIntroduction(club.getIntroduction());
        clubsAndFollow.setFoundingDate(club.getFoundingDate());
        clubsAndFollow.setClubId(club.getClubId());
        return R.ok(clubsAndFollow);
    }
    //根据社团名称模糊查询社团信息
    @PostMapping("/findClubsByclubName")
    public R findClubsByclubName(String clubName){
        List<Clubs> clubsList = clubsService.findClubsByclubName(clubName);
        if(clubsList.size()!=0)
        return R.ok(clubsList);
        else
            return R.error(500,"没有查询到任何相关社团信息");
    }

    @PostMapping("/findClubsByclubName1")
    public R findClubsByclubName1(String clubName){
        List<Clubs> clubsList = clubsService.findClubsByclubName(clubName);
        List<clubsAndLike> clubsAndLikes = new ArrayList<>();
        for (Clubs club : clubsList) {
            // 获取有多少个赞
            int likeNum = likeService.getLikeNum(club.getClubId());
            // 获取当前用户是否对这个社团点赞
            ClubLike clubLike = new ClubLike();
            int isLike = likeService.isLike(clubLike);
            // 创建ClubsAndLike对象，并将其添加到clubsAndLikes列表中
            clubsAndLike clubsAndLike = new clubsAndLike(club, likeNum, isLike);
            clubsAndLikes.add(clubsAndLike);
        }
        int total = clubsList.size();
        Map  result = new HashMap<>();
        result.put("clubsAndLikes",clubsAndLikes);
        result.put("total",total);
        return R.ok(result);
    }

    //查询社团数量
    @PostMapping("/findClubCount")
    public R findClubCount(){
        int count = clubsService.findClubCount();
        return R.ok(count);
    }

    //根据社团类型查询社团
    @PostMapping("/findClubByclubTid")
    public R findClubByclubTid(int clubTid){
        List<Clubs> clubsList = clubsService.findClubByclubTid(clubTid);
        if(clubsList.size()!=0)
        return R.ok(clubsList);
        else return R.error(500,"没此类社团");
    }
    //根据社团id获取社团招新信息
    @PostMapping("/findClubnewByclubId")
    public R findClubnewByclubId(int clubId){
        RecuritNew recuritNew = new RecuritNew();
        recuritNew = clubsService.findClubnewByclubId(clubId);
        if(recuritNew!=null)
        return R.ok(recuritNew);
        else return R.error(500,"没有招新公告");
    }
    //获取正在招新的社团列表
    @PostMapping("/findrecuritingnew")
    public R findrecuritingnew(pages pages){
        List<Clubs> recuritNews = clubsService.findrecuritingnew(pages);
        if(recuritNews.size()!=0)
            return R.ok(recuritNews);
        else return R.error(500,"当前没有招新的社团");
    }
    //获取正在招新社团总数
    @PostMapping("/findrecuritingnewNum")
    public R findrecuritingnewNum(){
        int num =clubsService.findrecuritingnewNum();
        return R.ok(num);
    }

    //获取是管理者的社团的id和名字
    @PostMapping("/findIsManagerClubIdAndName")
    public R findIsManagerClubIdAndName(int userId){
        List<clubidAndname> clubidAndnameList = clubsService.findIsManagerClubIdAndName(userId);
        if(clubidAndnameList.size()!=0)
            return  R.ok(clubidAndnameList);
        else return R.error(500,"没有管理的社团");
    }

    //添加社团招新公告
    @PostMapping("/addRecuritNew")
    public R addRecuritNew(RecuritNew recuritNew){
        try {
        clubsService.addRecuritNew(recuritNew);
        }catch (Exception e){
            System.out.println("出故障了"+e);
            return R.error(500,"添加失败");
        }
        return R.ok("添加成功");
    }
    //修改社团招新公告
    @PostMapping("/updateRecuritNew")
    public R updateRecuritNew(RecuritNew recuritNew){
        try {
            clubsService.updateRecuritNew(recuritNew);
        }catch (Exception e){
            System.out.println("出故障了"+e);
            return R.error(500,"修改失败");
        }
        return R.ok("修改成功");
    }
    //删除招新公告
    @PostMapping("/deleteRecuritNew")
    public R deleteRecuritNew(int clubId){
        try {
            clubsService.deleteRecuritNew(clubId);
        }catch (Exception e){
            System.out.println("出故障了"+e);
            return R.error(500,"删除失败");
        }
        return R.ok("删除成功");
    }

    //社团推荐
    @PostMapping("/clubrecommend")
    public R clubrecommend(int userId){
        List<Clubs> clubsList = clubsService.clubrecommend();
        List<clubsAndLike> clubsAndLikes = new ArrayList<>();
        for (Clubs club : clubsList) {
            // 获取有多少个赞
            int likeNum = likeService.getLikeNum(club.getClubId());
            // 获取当前用户是否对这个社团点赞
            ClubLike clubLike = new ClubLike();
            clubLike.setClubId(club.getClubId());
            clubLike.setUserId(userId);
            int isLike = likeService.isLike(clubLike);
            // 创建ClubsAndLike对象，并将其添加到clubsAndLikes列表中
            clubsAndLike clubsAndLike = new clubsAndLike(club, likeNum, isLike);
            clubsAndLikes.add(clubsAndLike);
        }
        return R.ok(clubsAndLikes);
    }
}
